package com.ruoyi.system.controller;

import com.ruoyi.basicsystem.domain.ZnDepot;
import com.ruoyi.common.utils.SecurityUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ZnMaterialProperty;
import com.ruoyi.system.service.IZnMaterialPropertyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品属性Controller
 * 
 * @author ruoyi
 * @date 2022-02-24
 */
@RestController
@RequestMapping("/system/materialproperty")
public class ZnMaterialPropertyController extends BaseController
{
    @Autowired
    private IZnMaterialPropertyService znMaterialPropertyService;

    /**
     * 查询商品属性列表
     */
    @PreAuthorize("@ss.hasPermi('system:materialproperty:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZnMaterialProperty znMaterialProperty)
    {
        startPage();
        List<ZnMaterialProperty> list = znMaterialPropertyService.selectZnMaterialPropertyList(znMaterialProperty);
        return getDataTable(list);
    }

    /**
     * 查询商品所有属性列表
     */

    @GetMapping("/listAll")
    public AjaxResult listAll(ZnMaterialProperty znMaterialProperty)
    {
       /* znMaterialProperty.setEnabled(1);*/
        List<ZnMaterialProperty> list = znMaterialPropertyService.selectZnMaterialPropertyList(znMaterialProperty);
        Collections.sort(list, new Comparator<ZnMaterialProperty>() {
            @Override
            public int compare(ZnMaterialProperty o1, ZnMaterialProperty o2) {
                if(StringUtils.isEmpty(o1.getSort())){
                    return -1;
                }
                if(StringUtils.isEmpty(o2.getSort())){
                    return -1;
                }
                return o1.getSort().compareTo(o2.getSort());
            }
        });
        return AjaxResult.success(list);
    }
    /**
     * 导出商品属性列表
     */
    @PreAuthorize("@ss.hasPermi('system:materialproperty:export')")
    @Log(title = "商品属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZnMaterialProperty znMaterialProperty)
    {
        List<ZnMaterialProperty> list = znMaterialPropertyService.selectZnMaterialPropertyList(znMaterialProperty);
        ExcelUtil<ZnMaterialProperty> util = new ExcelUtil<ZnMaterialProperty>(ZnMaterialProperty.class);
        util.exportExcel(response, list, "商品属性数据");
    }

    /**
     * 获取商品属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:materialproperty:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(znMaterialPropertyService.selectZnMaterialPropertyById(id));
    }

    /**
     * 新增商品属性
     */
    @PreAuthorize("@ss.hasPermi('system:materialproperty:add')")
    @Log(title = "商品属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZnMaterialProperty znMaterialProperty)
    {
        return toAjax(znMaterialPropertyService.insertZnMaterialProperty(znMaterialProperty));
    }

    /**
     * 修改商品属性
     */
    @PreAuthorize("@ss.hasPermi('system:materialproperty:edit')")
    @Log(title = "商品属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZnMaterialProperty znMaterialProperty)
    {
        return toAjax(znMaterialPropertyService.updateZnMaterialProperty(znMaterialProperty));
    }

    /**
     * 删除商品属性
     */
    @PreAuthorize("@ss.hasPermi('system:materialproperty:remove')")
    @Log(title = "商品属性", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(znMaterialPropertyService.deleteZnMaterialPropertyByIds(ids));
    }
}
